//
//  LBFLoatViewViewController.m
//  TEXT
//
//  Created by liubo on 2024/3/10.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "LBFLoatViewViewController.h"

@interface LBFLoatViewViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, assign) CGFloat lastOffset;

@end

@implementation LBFLoatViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headerView];
    // Do any additional setup after loading the view.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kkk"];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > self.lastOffset && CGRectGetMinY(self.headerView.frame) <= 50) {
        self.lastOffset = scrollView.contentOffset.y;
        return;
    }
    if (scrollView.contentOffset.y < self.lastOffset && CGRectGetMinY(self.headerView.frame) >= 100) {
        self.lastOffset = scrollView.contentOffset.y;
        return;
    }
    CGRect rect = self.headerView.frame;
    CGPoint orign = rect.origin;
    orign.y -=(scrollView.contentOffset.y - self.lastOffset);
    orign.y = MIN(100, MAX(50, orign.y));
    rect.origin = orign;
    self.headerView.frame = rect;
    self.lastOffset = scrollView.contentOffset.y;
}

#pragma mark - lazy load

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 100)];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"kkk"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 100 , CGRectGetWidth(self.view.frame), 50)];
        _headerView.backgroundColor = [UIColor cyanColor];
    }
    return _headerView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
